"""
参数管理系统 - FastAPI主应用
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .db import create_all_tables
from .config import CORS_ORIGINS
from .schemas import MessageResponse

# 导入路由模块
from .routers import (
    auth_router,
    projects_router,
    categories_router,
    parameters_router,
    parameter_values_router,
    export_import_router,
    validation_router,
    experiments_router,
)

# 触发插件注册（导入子包以执行注册装饰器）
from .io_formats import registry as _io_registry  # noqa: F401
from .io_formats.excel import exporter as _excel_exporter  # noqa: F401
from .io_formats.excel import importer as _excel_importer  # noqa: F401
from .io_formats.json import exporter as _json_exporter  # noqa: F401
from .io_formats.json import importer as _json_importer  # noqa: F401
from .io_formats.text import exporter as _text_exporter  # noqa: F401

# 创建FastAPI应用
app = FastAPI(
    title="参数管理系统",
    description="优化建模项目参数管理平台",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# 启用CORS，允许前端访问
app.add_middleware(
    CORSMiddleware,
    allow_origins=CORS_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

# 创建数据库表
create_all_tables()

# 注册路由
app.include_router(auth_router)
app.include_router(projects_router)
app.include_router(categories_router)
app.include_router(parameters_router)
app.include_router(parameter_values_router)
app.include_router(export_import_router)
app.include_router(validation_router)
app.include_router(experiments_router)


@app.get("/", response_model=MessageResponse)
async def root():
    """根路径"""
    return MessageResponse(message="参数管理系统API", success=True)


@app.get("/health", response_model=MessageResponse)
async def health_check():
    """健康检查接口"""
    return MessageResponse(message="系统运行正常", success=True)


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
